{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d60cec9-31d9-4d41-a832-a63173c25989",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4e87896f-ce1b-4ab1-ad48-300ee5080e87",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import sys\n",
    "# ..................................................\n",
    "import pandas\n",
    "# ..................................................\n",
    "import Python_3_HTTP_Collect\n",
    "import Python_3_URL_Path_Operate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6073a956-d212-4526-82fa-4b1d63de8409",
   "metadata": {},
   "outputs": [],
   "source": [
    "OBJ_Python_3_HTTP_Collect     = Python_3_HTTP_Collect.Python_3_HTTP_Collect()\n",
    "OBJ_Python_3_URL_Path_Operate = Python_3_URL_Path_Operate.Python_3_URL_Path_Operate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "57053cb1-ad32-4345-9cb4-20008ddc5614",
   "metadata": {},
   "outputs": [],
   "source": [
    "FILE_Json = open(\"HTTP_Headers_for_tsinghua.edu.cn.json\", mode = 'r', encoding = \"utf-8\")\n",
    "TEXT_Json = FILE_Json.read()\n",
    "FILE_Json.close()\n",
    "LIST_Json = json.loads(TEXT_Json)\n",
    "# ..................................................\n",
    "HTTP_Headers = LIST_Json[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b23c0a64-4872-4cf1-9657-99c8ef74698d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>version</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>chardet</td>\n",
       "      <td>5.2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>altgraph</td>\n",
       "      <td>0.17.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>packaging</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>pefile</td>\n",
       "      <td>2023.2.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>pyinstaller</td>\n",
       "      <td>6.16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>urllib3</td>\n",
       "      <td>2.5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>wcwidth</td>\n",
       "      <td>0.2.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>webcolors</td>\n",
       "      <td>25.10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>webencodings</td>\n",
       "      <td>0.5.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>websocket-client</td>\n",
       "      <td>1.9.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>102 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 name   version\n",
       "0             chardet     5.2.0\n",
       "1            altgraph    0.17.4\n",
       "2           packaging      25.0\n",
       "3              pefile  2023.2.7\n",
       "4         pyinstaller    6.16.0\n",
       "..                ...       ...\n",
       "97            urllib3     2.5.0\n",
       "98            wcwidth    0.2.14\n",
       "99          webcolors   25.10.0\n",
       "100      webencodings     0.5.1\n",
       "101  websocket-client     1.9.0\n",
       "\n",
       "[102 rows x 2 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Target_DataFrame = pandas.read_csv(\"./Target_PyPI_Packages.csv\", dtype = str)\n",
    "Target_DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "77392ccb-e797-4e52-869e-95f5908f25d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[DEBUG] Processing -> Package Name: websocket-client, Package Version: 1.9.0 (102/102)))/102)102)"
     ]
    }
   ],
   "source": [
    "LIST_Collected_All:list = []\n",
    "# ..................................................\n",
    "Total:int = Target_DataFrame[\"name\"].count()\n",
    "Count:int = 1\n",
    "# ..................................................\n",
    "OBJ_Python_3_HTTP_Collect.HTTP_Headers = HTTP_Headers\n",
    "# ..................................................\n",
    "for idx, row in Target_DataFrame.iterrows():\n",
    "    Package_Name:str    = row[\"name\"]\n",
    "    Package_Version:str = row[\"version\"]\n",
    "    # ..............................................\n",
    "    sys.stdout.write(f\"\\r[DEBUG] Processing -> Package Name: {Package_Name}, Package Version: {Package_Version} ({Count}/{Total})\")\n",
    "    sys.stdout.flush()\n",
    "    # ..............................................\n",
    "    URL = \"https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple\" + '/' + Package_Name\n",
    "    # ..............................................\n",
    "    LIST_Collected = OBJ_Python_3_HTTP_Collect.GET_Tags_a_Attrs_href(URL = URL)      # 获取 \"标签 a\" 的 \"属性 href\" 的值\n",
    "    LIST_Collected = [href for href in LIST_Collected if (Package_Version in href)]  # 筛选 Package 版本\n",
    "    # ..............................................\n",
    "    LIST_Collected_All.extend(LIST_Collected)\n",
    "    # ..............................................\n",
    "    Count = Count + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "72a9b8be-f72d-4285-936b-341d613b3168",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>url</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1066</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1067</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1068</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1069</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1070</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1071 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    url\n",
       "0     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "1     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "2     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "3     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "4     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "...                                                 ...\n",
       "1066  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "1067  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "1068  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "1069  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "1070  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...\n",
       "\n",
       "[1071 rows x 1 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Collected_DataFrame = pandas.DataFrame({\"url\": LIST_Collected_All})\n",
    "Collected_DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8662914a-1957-49b6-9c84-d2369352efff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>url</th>\n",
       "      <th>basename</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>chardet-5.2.0-py3-none-any.whl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>chardet-5.2.0.tar.gz</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>altgraph-0.17.4-py2.py3-none-any.whl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>altgraph-0.17.4.tar.gz</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>packaging-25.0-py3-none-any.whl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1066</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>webcolors-25.10.0.tar.gz</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1067</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>webencodings-0.5.1-py2.py3-none-any.whl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1068</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>webencodings-0.5.1.tar.gz</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1069</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>websocket_client-1.9.0-py3-none-any.whl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1070</th>\n",
       "      <td>https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...</td>\n",
       "      <td>websocket_client-1.9.0.tar.gz</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1071 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    url  \\\n",
       "0     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "1     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "2     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "3     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "4     https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "...                                                 ...   \n",
       "1066  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "1067  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "1068  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "1069  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "1070  https://mirrors.tuna.tsinghua.edu.cn/pypi/web/...   \n",
       "\n",
       "                                     basename  \n",
       "0              chardet-5.2.0-py3-none-any.whl  \n",
       "1                        chardet-5.2.0.tar.gz  \n",
       "2        altgraph-0.17.4-py2.py3-none-any.whl  \n",
       "3                      altgraph-0.17.4.tar.gz  \n",
       "4             packaging-25.0-py3-none-any.whl  \n",
       "...                                       ...  \n",
       "1066                 webcolors-25.10.0.tar.gz  \n",
       "1067  webencodings-0.5.1-py2.py3-none-any.whl  \n",
       "1068                webencodings-0.5.1.tar.gz  \n",
       "1069  websocket_client-1.9.0-py3-none-any.whl  \n",
       "1070            websocket_client-1.9.0.tar.gz  \n",
       "\n",
       "[1071 rows x 2 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Collected_DataFrame[\"basename\"] = Collected_DataFrame[\"url\"     ].apply(lambda x: OBJ_Python_3_URL_Path_Operate.Get_URL_Basename(x))\n",
    "Collected_DataFrame[\"basename\"] = Collected_DataFrame[\"basename\"].apply(lambda x: x.split('#')[0] if ('#' in x) else x)\n",
    "Collected_DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f610ed7e-c97c-466b-bc5e-a203ed4cf974",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[DEBUG] Processing -> Basename: websocket_client-1.9.0.tar.gz (1071/1071)1070/1071)1071)071))4.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1046/1071)"
     ]
    }
   ],
   "source": [
    "Total:int = Collected_DataFrame[\"url\"].count()\n",
    "Count:int = 1\n",
    "# ..................................................\n",
    "OBJ_Python_3_HTTP_Collect.HTTP_Headers = HTTP_Headers\n",
    "# ..................................................\n",
    "for idx, row in Collected_DataFrame.iterrows():\n",
    "    URL:str      = row[\"url\"     ]\n",
    "    Basename:str = row[\"basename\"]\n",
    "    # ..............................................\n",
    "    sys.stdout.write(f\"\\r[DEBUG] Processing -> Basename: {Basename} ({Count}/{Total})\")\n",
    "    sys.stdout.flush()\n",
    "    if (os.path.exists(\"./Collect_Downloads/\" + Basename) != True):\n",
    "        OBJ_Python_3_HTTP_Collect.GET_Download_File(URL = URL, Save_Path = \"./Collect_Downloads/\" + Basename)\n",
    "    # ..............................................\n",
    "    Count = Count + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2406f02f-55b1-4601-b6c9-019d755e8f3c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
